﻿<?php

/*
 *
 *
 * --------------------------------------------------------------------
 * Copyright (c) 2001 - 2011 Openfiler Project.
 * --------------------------------------------------------------------
 *
 * Openfiler is an Open Source SAN/NAS Appliance Software Distribution
 *
 * This file is part of Openfiler.
 *
 * Openfiler is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * Openfiler is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Openfiler.  If not, see <http://www.gnu.org/licenses/>.
 *
 * --------------------------------------------------------------------
 *
 *
 */

    require("pre.inc");
    require("md.inc");
    include("authenticated.inc");


    $md = get_md();

    $volumes_id = array();
    $volumes_name = array();
    $volumes_mountpoint = array();
    $volumes_vg = array();
    $volumes_fstype = array();

    function volumes_startelement($parser, $name, $attrs)
    {
            global $volumes_id, $volumes_name, $volumes_mountpoint, $volumes_vg, $volumes_fstype;

            if ($name == "VOLUME")
            {
                    array_push($volumes_id, $attrs["ID"]);
                    array_push($volumes_name, $attrs["NAME"]);
                    array_push($volumes_mountpoint, $attrs["MOUNTPOINT"]);
                    array_push($volumes_vg, $attrs["VG"]);
                    array_push($volumes_fstype, $attrs["FSTYPE"]);
            }
    }

    function volumes_endelement($parser, $name)
    {
    }

    $volumes_parser = xml_parser_create();
    xml_set_element_handler($volumes_parser, "volumes_startelement", "volumes_endelement");
    $volumes_fp = fopen("/opt/openfiler/etc/volumes.xml", "r");

    while ($volumes_data = fread($volumes_fp, 4096))
            xml_parse($volumes_parser, $volumes_data, feof($volumes_fp));

    fclose($volumes_fp);
    xml_parser_free($volumes_parser);

    $snapshots_id = array();
    $snapshots_lvname = array();
    $snapshots_vgname = array();
    $snapshots_shared = array();
    $snapshots_rotateid = array();

    $schedule_size = array();
    $schedule_share = array();
    $schedule_timecounter = array();
    $schedule_timemax = array();
    $schedule_rotatecounter = array();
    $schedule_rotatemax = array();
    $schedule_lvname = array();
    $schedule_vgname = array();

    function snapshots_startelement($parser, $name, $attrs)
    {
            global $snapshots_id, $snapshots_lvname, $snapshots_vgname, $snapshots_shared, $snapshots_rotateid;
            global $schedule_size, $schedule_share;
            global $schedule_timecounter, $schedule_timemax;
            global $schedule_rotatecounter, $schedule_rotatemax;
            global $schedule_lvname, $schedule_vgname;

            if ($name == "SNAPSHOT")
            {
                    array_push($snapshots_id, $attrs["ID"]);
                    array_push($snapshots_lvname, $attrs["LVNAME"]);
                    array_push($snapshots_vgname, $attrs["VGNAME"]);
                    array_push($snapshots_shared, $attrs["SHARED"]);
                    array_push($snapshots_rotateid, $attrs["ROTATEID"]);
            }
            else if ($name == "SCHEDULE")
            {
                    array_push($schedule_size, $attrs["SIZE"]);
                    array_push($schedule_share, $attrs["SHARE"]);
                    array_push($schedule_timecounter, $attrs["TIMECOUNTER"]);
                    array_push($schedule_timemax, $attrs["TIMEMAX"]);
                    array_push($schedule_rotatecounter, $attrs["ROTATECOUNTER"]);
                    array_push($schedule_rotatemax, $attrs["ROTATEMAX"]);
                    array_push($schedule_lvname, $attrs["LVNAME"]);
                    array_push($schedule_vgname, $attrs["VGNAME"]);
            }
    }

    function snapshots_endelement($parser, $name)
    {
    }

    $snapshots_parser = xml_parser_create();
    xml_set_element_handler($snapshots_parser, "snapshots_startelement", "snapshots_endelement");
    $snapshots_fp = fopen("/opt/openfiler/etc/snapshots.xml", "r");

    while ($snapshots_data = fread($snapshots_fp, 4096))
            xml_parse($snapshots_parser, $snapshots_data, feof($snapshots_fp));

    fclose($snapshots_fp);
    xml_parser_free($snapshots_parser);

    $disksp = popen("export LANG=C; /usr/bin/sudo /opt/openfiler/sbin/list-disks.pl", "r");
    $i = 0;
    while (!feof($disksp))
    {
            $dstr = trim(fgets($disksp, 4096));
            // $dresult[$i++] = substr($dstr, 0, strlen($dstr) - 1);
            $dresult[$i++] = $dstr;
    }
    pclose($disksp);

    $of_disks = array();

    if ($dresult[0] == "OPENFILER")
    {
            $i = 1;

            while ($i < count($dresult))
            {
                    if (strncmp($dresult[$i], "DISK", 4) == 0)
                    {
                            $p = $i;
                            $i = $i + 8;

                            $parts = array();

                            while (($dresult[$i] == "PARTITION") && ($i < count($dresult)))
                            {
                                    array_push($parts, array("device" => $dresult[$i + 1], "type" => $dresult[$i + 2], "number" => $dresult[$i + 3], "start" => $dresult[$i + 4], "end" => $dresult[$i + 5], "blocks" => $dresult[$i + 6], "extended" => $dresult[$i + 7]));
                                    $i = $i + 8;
                            }

                            array_push($of_disks,
                                    array("device" => $dresult[$p + 1],
                                            "type" => $dresult[$p + 2],
                                            "description" => $dresult[$p + 3],
                                            "blocksize" => ($dresult[$p + 4] * 2),
                                            "cylinders" => $dresult[$p + 5],
                                            "heads" => $dresult[$p + 6],
                                            "sectors" => $dresult[$p + 7],
                                            "partitions" => $parts));
                    }
                    else
                            break;
            }
    }

    $vgp = popen("export LANG=C; /usr/bin/sudo " . $GLOBALS["lvm_command_prefix"] . "vgdisplay -c", "r");
    $i = 0;
    while (!feof($vgp))
            $vgds[$i++] = fgets($vgp, 4096);
    pclose($vgp);


    $pvs_exist = false;
    $in_use = array();

    for ($i = 0; $i < count($of_disks); $i++)
    {
            for ($j = 0; $j < count($of_disks[$i]["partitions"]); $j++)
            {
                    $ptype = $of_disks[$i]["partitions"][$j]["type"];
                    $pv_device = $of_disks[$i]["partitions"][$j]["device"];

                    $in_use[$pv_device] = false;

                    if ($ptype == "8e")
                    {
                            $pvp = popen("export LANG=C; /usr/bin/sudo " . $GLOBALS["lvm_command_prefix"] . "pvdisplay " . $pv_device . " | grep \"Physical volume\" ", "r");
                            $pvstr = trim(fgets($pvp, 4096));
                            pclose($pvp);

                            if ((strcmp($pvstr, "--- Physical volume ---") == 0) || (strcmp($pvstr, "--- NEW Physical volume ---") == 0))
                            {
                                    $pvp = popen("export LANG=C; /usr/bin/sudo " . $GLOBALS["lvm_command_prefix"] . "pvdisplay -c " . $pv_device, "r");
                                    $pvstr = trim(fgets($pvp, 4096));
                                    pclose($pvp);

                                    $pvinfo = explode(":", trim($pvstr, " "));

                                    if (($pvinfo[0] == $pv_device) && (strlen($pvinfo[1]) > 0))
                                            $in_use[$pv_device] = true;
                                    else
                                            $pvs_exist = true;
                            }
                    }
            }
    }

    for ($i = 0; $i < count($md); $i++)
    {
            $pv_device = $md[$i]["device"];
            $in_use[$pv_device] = false;
            $pvp = popen("export LANG=C; /usr/bin/sudo " . $GLOBALS["lvm_command_prefix"] . "pvdisplay " . $pv_device . " | grep \"Physical volume\" ", "r");
            $pvstr = trim(fgets($pvp, 4096));
            pclose($pvp);



            if ((strcmp($pvstr, "--- Physical volume ---") == 0) || (strcmp($pvstr, "--- NEW Physical volume ---") == 0))
            {
                    $pvp = popen("export LANG=C; /usr/bin/sudo " . $GLOBALS["lvm_command_prefix"] . "pvdisplay -c " . $pv_device . " | tail -n1", "r");
                    $pvstr = trim(fgets($pvp, 4096));
                    pclose($pvp);

                    $pvinfo = explode(":", trim($pvstr, " "));

                    if (($pvinfo[0] == $pv_device) && (strlen($pvinfo[1]) > 0) && (strcmp($pvinfo[1], "#orphans_lvm2") != 0))
                            $in_use[$pv_device] = true;
                    else
                            $pvs_exist = true;

            }

            // the following will match a RAID device that's not a physical volume and prevent it's use in the vgmanage tool

            else if ((strcmp($pvstr, "--- Physical volume ---") != 0) || (strcmp($pvstr, "--- NEW Physical volume ---") != 0))
            {
                    $in_use[$pv_device] = true;
            }

    }

    if (isset($action) && ($action == "addpvs"))
    {
            $pvs = "";

            for ($i = 0; $i < count($of_disks); $i++)
            {
                    for ($j = 0; $j < count($of_disks[$i]["partitions"]); $j++)
                    {
                            $ptype = $of_disks[$i]["partitions"][$j]["type"];
                            $pv_device = $of_disks[$i]["partitions"][$j]["device"];

                            if (($ptype == "8e") && (!$in_use[$pv_device]) && (${"check" . sha1($pv_device)} == "on"))
                                    $pvs = $pvs . " " . $pv_device;
                    }
            }

            for ($i = 0; $i < count($md); $i++)
            {
                    $pv_device = $md[$i]["device"];

                    if ((!$in_use[$pv_device]) && (${"check" . sha1($pv_device)} == "on"))
                            $pvs = $pvs . " " . $pv_device;
            }

            for ($i = 0; $i < count($vgds); $i++)
            {
                    $vginfo = explode(":", trim($vgds[$i], " "));

                    if ($vginfo[11] <= 0)
                            continue;

                    if ($vginfo[0] == $vg)
                    {
                            exec("export LANG=C; /usr/bin/sudo " . $GLOBALS["lvm_command_prefix"] . "vgextend " . escapeshellarg($vg) . $pvs);
                            break;
                    }
            }

            header("Location: ./volumes_vgmanage.html");
            exit;
    }
    else if (isset($action) && ($action == "createvg"))
    {
            $pvs = "";

            for ($i = 0; $i < count($of_disks); $i++)
            {
                    for ($j = 0; $j < count($of_disks[$i]["partitions"]); $j++)
                    {
                            $ptype = $of_disks[$i]["partitions"][$j]["type"];
                            $pv_device = $of_disks[$i]["partitions"][$j]["device"];

                            if (($ptype == "8e") && (!$in_use[$pv_device]) && (${"check" . sha1($pv_device)} == "on"))
                                    $pvs = $pvs . " " . $pv_device;
                    }
            }

            for ($i = 0; $i < count($md); $i++)
            {
                    $pv_device = $md[$i]["device"];

                    if ((!$in_use[$pv_device]) && (${"check" . sha1($pv_device)} == "on"))
                            $pvs = $pvs . " " . $pv_device;
            }

            $vg_found = false;

            for ($i = 0; $i < count($vgds); $i++)
            {
                    $vginfo = explode(":", trim($vgds[$i], " "));

                    if ($vginfo[11] <= 0)
                            continue;

                    if ($vginfo[0] == $vgname)
                    {
                            $vg_found = true;
                            break;
                    }
            }

            if ((!$vg_found) && (is_valid_foldername($vgname)))
                    exec("export LANG=C; /usr/bin/sudo ". $GLOBALS["lvm_command_prefix"] . "vgcreate -s 32M " . escapeshellarg(strtolower($vgname)) . $pvs);

            header("Location: ./volumes_vgmanage.html");
            exit;
    }
    else if (isset($action) && ($action == "deletevg"))
    {
            $volume_group_in_use = false;

            for ($v = 0; $v < count($volumes_vg); $v++)
                    if ($volumes_vg[$v] == $vg)
                            $volume_group_in_use = true;

                    if (!$volume_group_in_use)
                    {
                            exec("export LANG=C; /usr/bin/sudo " . $GLOBALS["lvm_command_prefix"] . "vgchange -a n " . escapeshellarg($vg));
                            exec("export LANG=C; /usr/bin/sudo " . $GLOBALS["lvm_command_prefix"] . "vgremove " . escapeshellarg($vg));

                            if (is_dir("/mnt/" . $vg))
                                    exec("export LANG=C; /usr/bin/sudo /bin/rmdir /mnt/" . escapeshellarg($vg));

                    }

            header("Location: ./volumes_vgmanage.html");
            exit;
    }

    generic_header(array("title" => _("OPENFILER : 存储卷 : 存储卷管理")));
    single_begin(array());

?>

<script src="utility.js" type="text/javascript" language="JavaScript">
</script>
<script src="popup.js" type="text/javascript" language="JavaScript">
</script>

<p>&nbsp;</p>

<?php
        nested_tab_begin("C_VOLUME_GROUP_MANAGEMENT");
	print("<h3 align=\"center\">Volume Group Management</h3>\n");

	if (count($vgds) > 0)
	{
		if ($pvs_exist)
		{
			print("<form name=\"addpvform\" method=\"post\" action=\"volumes_vgmanage.html\">\n");
			print("<input type=\"hidden\" name=\"action\" value=\"addpvs\" />\n");
			print("<input type=\"hidden\" name=\"vg\" value=\"\" />\n");

			print("<div id=\"popup\" onclick=\"event.cancelBubble = true;\" class=\"pvspopup\">\n");
			print("<table cellpadding=\"8\" cellspacing=\"2\" border=\"0\">\n");
			print("<tr>\n");
			print("\t<td colspan=\"3\" bgcolor=\"" . $GLOBALS["color_table_heading"] . "\" align=\"center\"><strong>Select physical volumes to add</strong></td>\n");
			print("</tr>\n");

			$dcolor = 0;

			for ($i = 0; $i < count($of_disks); $i++)
			{
				for ($j = 0; $j < count($of_disks[$i]["partitions"]); $j++)
				{
					$ptype = $of_disks[$i]["partitions"][$j]["type"];
					$pv_device = $of_disks[$i]["partitions"][$j]["device"];

					if (($ptype == "8e") && (!$in_use[$pv_device]))
					{
						if ($dcolor == 0)
						{
							$dcolor = 1;
							$dvalue = $GLOBALS["color_table_row1"];
						}
						else
						{
							$dcolor = 0;
							$dvalue = $GLOBALS["color_table_row2"];
						}

						$part_size = ($of_disks[$i]["blocksize"] * $of_disks[$i]["partitions"][$j]["blocks"]);

						if ($part_size > (1024 * 1024 * 1024))
							$part_size_string = sprintf("%.02f GB", ($part_size / (1024 * 1024 * 1024.0)));
						else if ($part_size > (1024 * 1024))
							$part_size_string = sprintf("%.02f MB", ($part_size / (1024 * 1024.0)));
						else if ($part_size > (1024))
							$part_size_string = sprintf("%.02f KB", ($part_size / (1024.0)));
						else
							$part_size_string = $part_size . " bytes";

						print("<tr>\n");
						print("<td bgcolor=\"" . $dvalue . "\" align=\"left\"><input type=\"checkbox\" name=\"check" . sha1($pv_device) . "\" /></td>\n");
						print("<td bgcolor=\"" . $dvalue . "\" align=\"left\">" . $pv_device . "</td>\n");
						print("<td bgcolor=\"" . $dvalue . "\" align=\"left\">" . $part_size_string . "</td>\n");
						print("</tr>\n");
					}
				}
			}

			for ($i = 0; $i < count($md); $i++)
			{
				$pv_device = $md[$i]["device"];

				if (!$in_use[$pv_device])
				{
					if ($dcolor == 0)
					{
						$dcolor = 1;
						$dvalue = $GLOBALS["color_table_row1"];
					}
					else
					{
						$dcolor = 0;
						$dvalue = $GLOBALS["color_table_row2"];
					}

					$part_size = $md[$i]["array_size"];

					if ($part_size > (1024 * 1024 * 1024))
						$part_size_string = sprintf("%.02f GB", ($part_size / (1024 * 1024 * 1024.0)));
					else if ($part_size > (1024 * 1024))
						$part_size_string = sprintf("%.02f MB", ($part_size / (1024 * 1024.0)));
					else if ($part_size > (1024))
						$part_size_string = sprintf("%.02f KB", ($part_size / (1024.0)));
					else
						$part_size_string = $part_size . " bytes";

					print("<tr>\n");
					print("<td bgcolor=\"" . $dvalue . "\" align=\"left\"><input type=\"checkbox\" name=\"check" . sha1($pv_device) . "\" /></td>\n");
					print("<td bgcolor=\"" . $dvalue . "\" align=\"left\">" . $pv_device . "</td>\n");
					print("<td bgcolor=\"" . $dvalue . "\" align=\"left\">" . $part_size_string . "</td>\n");
					print("</tr>\n");
				}
			}

			print("<tr>\n");
			print("\t<td colspan=\"3\" bgcolor=\"" . $GLOBALS["color_table_heading"] . "\" align=\"center\"><input type=\"submit\" value=\"Submit\" /></td>\n");
			print("</tr>\n");
			print("</table>\n");

			print("<p align=\"center\"><a href=\"#\" onclick=\"hideCurrentPopup(); return false;\">Close Window</a></p>\n");

			print("</div>\n");
			print("</form>\n");
		}

		for ($ii = 0; $ii < count($vgds); $ii++)
		{
			$vginfo = explode(":", trim($vgds[$ii], " "));

			if ((!isset($vginfo[11])) || ($vginfo[11] <= 0))
				continue;

			print("<div id=\"popup-" . $vginfo[0] . "\" onclick=\"event.cancelBubble = true;\" class=\"pvspopup\">\n");
			print("<table cellpadding=\"8\" cellspacing=\"2\" border=\"0\">\n");
			print("<tr>\n");
			print("\t<td colspan=\"2\" bgcolor=\"" . $GLOBALS["color_table_heading"] . "\" align=\"center\"><strong>Member PVs of VG &ldquo;" . $vginfo[0] . "&rdquo;</strong></td>\n");
			print("</tr>\n");
			print("<tr>\n");
			print("\t<td bgcolor=\"" . $GLOBALS["color_table_heading"] . "\" align=\"left\"><strong>Device</strong></td>\n");
			print("\t<td bgcolor=\"" . $GLOBALS["color_table_heading"] . "\" align=\"left\"><strong>Size</strong></td>\n");
			print("</tr>\n");

			$dcolor = 0;

			for ($i = 0; $i < count($of_disks); $i++)
			{
				for ($j = 0; $j < count($of_disks[$i]["partitions"]); $j++)
				{
					$ptype = $of_disks[$i]["partitions"][$j]["type"];
					$pv_device = $of_disks[$i]["partitions"][$j]["device"];

					if ($ptype == "8e")
					{
						$pvp = popen("export LANG=C; /usr/bin/sudo " . $GLOBALS["lvm_command_prefix"] . "pvdisplay -c " . $pv_device, "r");
						$pvstr = trim(fgets($pvp, 4096));
						pclose($pvp);
					}
					else
						continue;

					$pvinfo = explode(":", trim($pvstr, " "));

					if ($pvinfo[0] != $pv_device)
						continue;

					if ($pvinfo[1] != $vginfo[0])
						continue;

					if ($dcolor == 0)
					{
						$dcolor = 1;
						$dvalue = $GLOBALS["color_table_row1"];
					}
					else
					{
						$dcolor = 0;
						$dvalue = $GLOBALS["color_table_row2"];
					}
                                        
                                        
                                        $tmpH = popen('export LANG=C; /usr/bin/sudo /sbin/blockdev --getsize64 2>&1 ' . $pv_device , 'r');
                                        $part_size = fread($tmpH, 2096);
                                        pclose($tmpH); 
                                        
                                        if ($part_size > (1024 * 1024 * 1024 * 1024))
						$part_size_string = sprintf("%.02f TB", ($part_size / (1024 * 1024 * 1024 * 1024.0)));
					else if ($part_size > (1024 * 1024 * 1024))
						$part_size_string = sprintf("%.02f GB", ($part_size / (1024 * 1024 * 1024.0)));
					else if ($part_size > (1024 * 1024))
						$part_size_string = sprintf("%.02f MB", ($part_size / (1024 * 1024.0)));
					else if ($part_size > (1024))
						$part_size_string = sprintf("%.02f KB", ($part_size / (1024.0)));
					else
						$part_size_string = $part_size . " bytes";
						
                                        

					print("<tr>\n");
					print("<td bgcolor=\"" . $dvalue . "\" align=\"left\">" . $pv_device . "</td>\n");
					print("<td bgcolor=\"" . $dvalue . "\" align=\"left\">" . $part_size_string . "</td>\n");
					print("</tr>\n");
				}
			}

			for ($i = 0; $i < count($md); $i++)
			{
					$pv_device = $md[$i]["device"];

					$pvp = popen("export LANG=C; /usr/bin/sudo " . $GLOBALS["lvm_command_prefix"] . "pvdisplay -c " . $pv_device, "r");
					$pvstr = trim(fgets($pvp, 4096));
					pclose($pvp);

					$pvinfo = explode(":", trim($pvstr, " "));

					if ($pvinfo[0] != $pv_device)
						continue;

					if ($pvinfo[1] != $vginfo[0])
						continue;

					if ($dcolor == 0)
					{
						$dcolor = 1;
						$dvalue = $GLOBALS["color_table_row1"];
					}
					else
					{
						$dcolor = 0;
						$dvalue = $GLOBALS["color_table_row2"];
					}

					$part_size = $md[$i]["array_size"];

					if ($part_size > (1024 * 1024 * 1024))
						$part_size_string = sprintf("%.02f GB", ($part_size / (1024 * 1024 * 1024.0)));
					else if ($part_size > (1024 * 1024))
						$part_size_string = sprintf("%.02f MB", ($part_size / (1024 * 1024.0)));
					else if ($part_size > (1024))
						$part_size_string = sprintf("%.02f KB", ($part_size / (1024.0)));
					else
						$part_size_string = $part_size . " bytes";

					print("<tr>\n");
					print("<td bgcolor=\"" . $dvalue . "\" align=\"left\">" . $pv_device . "</td>\n");
					print("<td bgcolor=\"" . $dvalue . "\" align=\"left\">" . $part_size_string . "</td>\n");
					print("</tr>\n");
			}

			print("</table>\n");
			print("<p align=\"center\"><a href=\"#\" onclick=\"hideCurrentPopup(); return false;\">Close Window</a></p>\n");
			print("</div>\n");
		}

		print("<div align=\"center\">\n");
		print("<table cellpadding=\"8\" cellspacing=\"2\" border=\"0\">\n");
		print("<tr>\n");
		print("\t<td bgcolor=\"" . $GLOBALS["color_table_heading"] . "\"><strong>Volume Group Name</strong></td>\n");
		print("\t<td bgcolor=\"" . $GLOBALS["color_table_heading"] . "\"><strong>Size</strong></td>\n");
		print("\t<td bgcolor=\"" . $GLOBALS["color_table_heading"] . "\"><strong>Allocated</strong></td>\n");
		print("\t<td bgcolor=\"" . $GLOBALS["color_table_heading"] . "\"><strong>Free</strong></td>\n");
		print("\t<td bgcolor=\"" . $GLOBALS["color_table_heading"] . "\" align=\"center\"><strong>Members</strong></td>\n");
		print("\t<td bgcolor=\"" . $GLOBALS["color_table_heading"] . "\" align=\"center\"><strong>Add physical storage</strong></td>\n");
		print("\t<td bgcolor=\"" . $GLOBALS["color_table_heading"] . "\" align=\"center\"><strong>Delete VG</strong></td>\n");
		print("</tr>\n");

		$dcolor = 0;

		for ($ii = 0; $ii < count($vgds); $ii++)
		{
			$vginfo = explode(":", trim($vgds[$ii]));

			if ((!isset($vginfo[11])) || (intval($vginfo[11]) <= 0))
				continue;

			if ($dcolor == 0)
			{
				$dcolor = 1;
				$dvalue = $GLOBALS["color_table_row1"];
			}
			else
			{
				$dcolor = 0;
				$dvalue = $GLOBALS["color_table_row2"];
			}

			print("<tr>\n");
			print("<td bgcolor=\"" . $dvalue . "\" align=\"left\">" . htmlentities($vginfo[0]) . "</td>\n");

			$vg_size = $vginfo[11] * 1024;

			if ($vg_size > (1024 * 1024 * 1024))
				$vg_size_string = sprintf("%.02f GB", ($vg_size / (1024 * 1024 * 1024.0)));
			else if ($vg_size > (1024 * 1024))
				$vg_size_string = sprintf("%.02f MB", ($vg_size / (1024 * 1024.0)));
			else if ($vg_size > (1024))
				$vg_size_string = sprintf("%.02f KB", ($vg_size / (1024.0)));
			else
				$vg_size_string = $vg_size . " bytes";

			print("<td bgcolor=\"" . $dvalue . "\" align=\"left\">" . htmlentities($vg_size_string) . "</td>\n");

			$vg_size = $vginfo[12] * $vginfo[14] * 1024;

			if ($vg_size > (1024 * 1024 * 1024))
				$vg_size_string = sprintf("%.02f GB", ($vg_size / (1024 * 1024 * 1024.0)));
			else if ($vg_size > (1024 * 1024))
				$vg_size_string = sprintf("%.02f MB", ($vg_size / (1024 * 1024.0)));
			else if ($vg_size > (1024))
				$vg_size_string = sprintf("%.02f KB", ($vg_size / (1024.0)));
			else
				$vg_size_string = $vg_size . " bytes";

			print("<td bgcolor=\"" . $dvalue . "\" align=\"left\">" . htmlentities($vg_size_string) . "</td>\n");

			$vg_size = $vginfo[12] * $vginfo[15] * 1024;

			if ($vg_size > (1024 * 1024 * 1024))
				$vg_size_string = sprintf("%.02f GB", ($vg_size / (1024 * 1024 * 1024.0)));
			else if ($vg_size > (1024 * 1024))
				$vg_size_string = sprintf("%.02f MB", ($vg_size / (1024 * 1024.0)));
			else if ($vg_size > (1024))
				$vg_size_string = sprintf("%.02f KB", ($vg_size / (1024.0)));
			else
				$vg_size_string = $vg_size . " bytes";

			print("<td bgcolor=\"" . $dvalue . "\" align=\"left\">" . htmlentities($vg_size_string) . "</td>\n");
			print("<td bgcolor=\"" . $dvalue . "\" align=\"left\"><a href=\"#\" onclick=\"return !showPopup('popup-" . $vginfo[0] . "', event);\">View member PVs</a></td>\n");
			print("<td bgcolor=\"" . $dvalue . "\" align=\"center\">");

			if ($pvs_exist)
				print("<a href=\"#\" onclick=\"document.addpvform.vg.value='" . $vginfo[0] . "';return !showPopup('popup', event);\">Add PVs</a>");
			else
				print("<em>All PVs are used</em>");

			print("</td>\n");

			$volume_group_in_use = false;

			for ($v = 0; $v < count($volumes_vg); $v++)
				if ($volumes_vg[$v] == $vginfo[0])
					$volume_group_in_use = true;

			if ($volume_group_in_use)
				print("<td bgcolor=\"" . $dvalue . "\" align=\"center\"><em><a href=\"volumes.html?vgname=" . $vginfo[0] . "\">VG contains volumes</a></em></td>\n");
			else
				print("<td bgcolor=\"" . $dvalue . "\" align=\"center\"><a href=\"volumes_vgmanage.html?action=deletevg&amp;vg=" . $vginfo[0] . "\">Delete</a></td>\n");

			print("</tr>\n");
		}

		print("</table>\n");
		print("</div>\n");
	}
	else
		printMessageBlock("warning","No existing volume groups were found. Please create a volume group using the physical volumes listed below.");


	print("<p align=\"center\">&nbsp;</p>\n");

	print("<hr />\n");

	print("<h3 align=\"center\">Create a new volume group</h3>\n");


	if ($pvs_exist)
	{
            printMessageBlock("info","<p>Valid characters for volume group name: <b>A-Z a-z 0-9 _ + -</b></p>");
		print("<form method=\"post\" action=\"volumes_vgmanage.html\">\n");
		print("<input type=\"hidden\" name=\"action\" value=\"createvg\" />\n");

		print("<div align=\"center\">\n");
		print("<table cellpadding=\"8\" cellspacing=\"2\" border=\"0\">\n");
		print("<tr>\n");
		print("\t<td colspan=\"3\" bgcolor=\"" . $GLOBALS["color_table_heading"] . "\" align=\"center\"><strong>Volume group name (no spaces)</strong></td>\n");
		print("</tr>\n");
		print("<tr>\n");
		print("\t<td colspan=\"3\" bgcolor=\"" . $GLOBALS["color_table_row2"] . "\" align=\"center\"><input type=\"text\" name=\"vgname\" /></td>\n");
		print("</tr>\n");
		print("<tr>\n");
		print("\t<td colspan=\"3\" bgcolor=\"" . $GLOBALS["color_table_heading"] . "\" align=\"center\"><strong>Select physical volumes to add</strong></td>\n");
		print("</tr>\n");

		$dcolor = 0;

		for ($i = 0; $i < count($of_disks); $i++)
		{
			for ($j = 0; $j < count($of_disks[$i]["partitions"]); $j++)
			{
				$ptype = $of_disks[$i]["partitions"][$j]["type"];
				$pv_device = $of_disks[$i]["partitions"][$j]["device"];

				if (($ptype == "8e") && (!$in_use[$pv_device]))
				{
					if ($dcolor == 0)
					{
						$dcolor = 1;
						$dvalue = $GLOBALS["color_table_row1"];
					}
					else
					{
						$dcolor = 0;
						$dvalue = $GLOBALS["color_table_row2"];
					}

					$part_size = ($of_disks[$i]["blocksize"] * $of_disks[$i]["partitions"][$j]["blocks"]);

					if ($part_size > (1024 * 1024 * 1024))
						$part_size_string = sprintf("%.02f GB", ($part_size / (1024 * 1024 * 1024.0)));
					else if ($part_size > (1024 * 1024))
						$part_size_string = sprintf("%.02f MB", ($part_size / (1024 * 1024.0)));
					else if ($part_size > (1024))
						$part_size_string = sprintf("%.02f KB", ($part_size / (1024.0)));
					else
						$part_size_string = $part_size . " bytes";

					print("<tr>\n");
					print("<td bgcolor=\"" . $dvalue . "\" align=\"left\"><input type=\"checkbox\" name=\"check" . sha1($pv_device) . "\" /></td>\n");
					print("<td bgcolor=\"" . $dvalue . "\" align=\"left\">" . $pv_device . "</td>\n");
					print("<td bgcolor=\"" . $dvalue . "\" align=\"left\">" . $part_size_string . "</td>\n");
					print("</tr>\n");
				}
			}
		}

		for ($i = 0; $i < count($md); $i++)
		{
			$pv_device = $md[$i]["device"];

			if (!$in_use[$pv_device])
			{
				if ($dcolor == 0)
				{
					$dcolor = 1;
					$dvalue = $GLOBALS["color_table_row1"];
				}
				else
				{
					$dcolor = 0;
					$dvalue = $GLOBALS["color_table_row2"];
				}

				$part_size = $md[$i]["array_size"];

				if ($part_size > (1024 * 1024 * 1024))
					$part_size_string = sprintf("%.02f GB", ($part_size / (1024 * 1024 * 1024.0)));
				else if ($part_size > (1024 * 1024))
					$part_size_string = sprintf("%.02f MB", ($part_size / (1024 * 1024.0)));
				else if ($part_size > (1024))
					$part_size_string = sprintf("%.02f KB", ($part_size / (1024.0)));
				else
					$part_size_string = $part_size . " bytes";

				print("<tr>\n");
				print("<td bgcolor=\"" . $dvalue . "\" align=\"left\"><input type=\"checkbox\" name=\"check" . sha1($pv_device) . "\" /></td>\n");
				print("<td bgcolor=\"" . $dvalue . "\" align=\"left\">" . $pv_device . "</td>\n");
				print("<td bgcolor=\"" . $dvalue . "\" align=\"left\">" . $part_size_string . "</td>\n");
				print("</tr>\n");
			}
		}

		print("<tr>\n");
		print("\t<td colspan=\"3\" bgcolor=\"" . $GLOBALS["color_table_heading"] . "\" align=\"center\"><input type=\"submit\" value=\"Add volume group\" /></td>\n");
		print("</tr>\n");

		print("</table>\n");
		print("</div>\n");

		print("</form>\n");

	}
	else
		printMessageBlock("warning", "<p align=\"center\"><em>No existing physical volumes were found, or all existing physical volumes are used. You can <a href=\"volumes_physical.html\">create new physical volumes</a>.</em></p>\n");


	print("<p align=\"center\">&nbsp;</p>\n");
	nested_tab_end();

	single_end(array());
	generic_footer(array());
?>
